package dao

import javax.inject.Inject
import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future


/**
 * Created by yz on 2018/5/29
 */
class ConfigDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  def selectAll(userId: Int) = db.run(Config.filter(_.userId === userId).result)

  def select(id: Int) = db.run(Config.filter(_.id === id).result.head)

  def selectByNameOp(userId: Int, name: String) = db.run(Config.
    filter(_.userId === userId).filter(_.name === name).result.headOption)

  def insert(row: ConfigRow) = db.run(Config += row).map(_ => ())

  def insertAndReturnId(row: ConfigRow) = db.run(Config.returning(Config.map(_.id)) += row)

  def deleteById(id: Int) = db.run(Config.filter(_.id === id).delete).map(_ => ())


}
